CreateXfsqController = function(){
	
	
	this.initPageData = function(){	
		var transferParam = CommonUtil.takeOutParamObject();
		if(!CommonUtil.isNotBlank(transferParam)){
			CommonUtil.dialogWarn("跳转页面参数异常!");
			return;
		}else{		
			//初始化数据到创建活动全局参数变量;
			this.initActivityPublicParam();			
			//渲染页面动态数据;
			this.renderHtmlData();
			
		}
		
	};
	
 
	 /**
     * 页面首次加载初始化活动基础数据;
     */
	this.initActivityPublicParam = function(){
		var transferParam = CommonUtil.takeOutParamObject();
		createActivityPublicParam.activityType = transferParam.activityType;
		createActivityPublicParam.voucherStartTime = DateUtil.getDateFormat(new Date());
		createActivityPublicParam.voucherEndTime = DateUtil.getDateFormat(DateUtil.addDays(new Date(),30));
        createActivityPublicParam.voucherRelativeTime = 30;
        createActivityPublicParam.isRelativeTime = true;    
    	createActivityPublicParam.autoDelayFlag  = "Y";//默认相对时间;
    	createActivityPublicParam.voucherType =ACTIVITY_TICKET_TYPE.ALL_TICKET;
	}	
	
	/**
	 * 绑定活动日期联动控件;	
	 */
	this.bindVoucherTimeWidget = function(){	
		ActivityUtil.loadTimeWidget(true,function(){
			var activityDateParams = createActivityPublicParam.selectDateParams;
			activityDateParams.dateType = "voucherDate";
			activityDateParams.pageId = "createXfsqActivityPage";
			activityDateParams.beginDateInputId = "voucher-date-start";
			activityDateParams.endDateInputId = "voucher-date-end";
			activityDateParams.selectBeginDateFunc = function(selectTime){
				if(selectTime != createActivityPublicParam.voucherStartTime){
					createActivityPublicParam.voucherStartTime = selectTime;
					createActivityPublicParam.voucherEndTime = DateUtil.getDateFormat(DateUtil.addDays(DateUtil.str2Date(selectTime),30));
		        	$("#createXfsqActivityPage #voucher-date-end").val("");
		        	ActivityUtil.loadTimeWidget(true);
				}
			};
			activityDateParams.selectEndDateFunc = function(selectTime){
				if(selectTime != createActivityPublicParam.voucherEndTime){
	            	createActivityPublicParam.voucherEndTime = selectTime;
	            	ActivityUtil.loadTimeWidget(true);
				}		
			};
			
		});
	}

   
	/**
	 * 保存券数据;
	 */
	this.saveVoucherData = function(_index){
		var isRelativeTime = ($("#createXfsqActivityPage #quan-deadline").find("span").text()=="相对时间")?true:false;
		var voucherData = {
			"receiveConditionMoney" : parseFloat($("#createXfsqActivityPage #receiveConditionMoney").val()).toFixed(2),
			"voucherWorthValue" :  parseFloat($("#createXfsqActivityPage #voucherAmount").val()).toFixed(2),
			"voucherMinConsume" : parseFloat($("#createXfsqActivityPage #voucherMinConsumeAmount").val()).toFixed(2),
			"isRelativeTime" : isRelativeTime,
			"voucherRelativeTime":($("#createXfsqActivityPage #relativeValidDays").val()=="")?30:$("#createXfsqActivityPage #relativeValidDays").val(),
			"voucherStartTime" : $("#createXfsqActivityPage #voucher-date-start").val(),
			"voucherEndTime" : $("#createXfsqActivityPage #voucher-date-end").val(),
			"voucherSubTitle":"",
		    "voucherLogoUri":"",
		    "voucherLogo":""
		};
		 
		if(_index && _index!=undefined){
			createActivityPublicParam.voucherData[_index]=voucherData;
		}else{		
			createActivityPublicParam.voucherData.push(voucherData);
		}
	} 
	
	this.showVoucherData = function(_index){
		XfsqConstants.voucherIndex = _index;
		var voucherDataArray = createActivityPublicParam.voucherData;
		var voucherData = voucherDataArray[_index];
		$("#createXfsqActivityPage .form-title").text("设置优惠券");
		$("#createXfsqActivityPage #receiveConditionMoney").val(voucherData.receiveConditionMoney);
		$("#createXfsqActivityPage #voucherAmount").val(voucherData.voucherWorthValue);
		$("#createXfsqActivityPage #voucherMinConsumeAmount").val(voucherData.voucherMinConsume);
	}
	
	/**
	 * 清理数据显示;
	 */
	this.clearVoucherPageData =function(){
		$("#createXfsqActivityPage .form-title").text("添加优惠券");
		$("#createXfsqActivityPage #receiveConditionMoney").val("");
		$("#createXfsqActivityPage #voucherAmount").val("");
		$("#createXfsqActivityPage #voucherMinConsumeAmount").val("");
 	}
	
	/**
	 * 渲染页面显示;
	 */
	this.renderHtmlData = function(){
		//渲染模版动态数据;
        CommonUtil.render($("#createXfsqActivityPage #xfsqActivityDataContainer"), "activity_xfsq_data_tpl",createActivityPublicParam);	
        //绑定活动日期联动控件;
		this.bindVoucherTimeWidget();	
	 				
	}

	/**
	 * 检查代金券信息;
	 */
	this.checkVoucherData = function(){	
		var voucherData = createActivityPublicParam.voucherData;
	    if(voucherData.length == 0){
	    	DialogUtil.dialogWarn("请添加优惠券!");
	    	return false;
	    }
		return true;
	}
	/**
	 * 验证消费送代金券的领取条件金额是否重复;
	 */
	this.checkReceiveCondition = function(receiveConditionMoney){
		var voucherData = createActivityPublicParam.voucherData;
		if(voucherData.length == 0){
			return true;
		}
		for(var i=0;i<voucherData.length;i++){
			if(receiveConditionMoney==voucherData[i].receiveConditionMoney){
				return false;
			}
		}
		return true;
		
		
	}
	
	this.checkNewVoucherData = function(){
		var receiveConditionMoney = $("#createXfsqActivityPage #receiveConditionMoney").val("");
		var voucherAmount = $("#createXfsqActivityPage #voucherAmount").val("");
		var voucherMinConsumeAmount = $("#createXfsqActivityPage #voucherMinConsumeAmount").val("");
		
		if(receiveConditionMoney==""){
			DialogUtil.dialogWarn("请填写券领取条件!");
			return false;
		}
		if(isNaN(receiveConditionMoney)){
			DialogUtil.dialogWarn("券领取条件必须为数字");
			return false;
		}
		if(receiveConditionMoney.indexOf(".")!=-1){
			DialogUtil.dialogWarn("券领取条件必须为整型数字");
			return false;
		}
		if(receiveConditionMoney>5000){
			DialogUtil.dialogWarn("券领取条件需要小于5000");
			return false;
		}	
		
		return true;
		
	}
 
    
	
};